export const userFormInit = {
  visit: false,
  title: '新建用户',
  createOrUpdate: false,
};

export const roleFormInit = {
  visit: false, //绑定角色弹框是否显示
  title: '绑定角色', //设置绑定弹框的名称
  userData: { id: '' }, //用户信息
  rolesData: [{ title: '标题', key: '' }], //全部角色信息
  targetKeys: [], //穿梭框的目标值
};

export function userFormReducer(state: any, action: any) {
  switch (action.type) {
    case 'create': {
      return {
        ...state,
        title: '新建用户',
        createOrUpdate: false,
        visit: true,
      };
    }
    case 'update': {
      return {
        ...state,
        title: '更新用户',
        createOrUpdate: true,
        visit: true,
      };
    }
    case 'open': {
      return {
        ...state,
        visit: true,
      };
    }
    case 'close': {
      return {
        ...state,
        visit: false,
      };
    }
    default:
      return state;
  }
}

export function roleFormReducer(state: any, action: any) {
  switch (action.type) {
    // 打开穿梭框的设置
    case 'open': {
      return {
        ...state,
        title: '绑定角色',
        userData: { id: action.userId },
      };
    }
    case 'setUserId': {
      return {
        ...state,
        userData: { id: action.userId },
      };
    }
    case 'loadData': {
      return {
        ...state,
        rolesData: action.rolesData,
        targetKeys: action.targetKeys,
        visit: true,
      };
    }
    case 'onchange': {
      return {
        ...state,
        targetKeys: action.targetKeys,
      };
    }
    case 'close': {
      return {
        ...state,
        visit: false,
      };
    }
    default:
      return state;
  }
}
